package cn.iocoder.yudao.module.dr.controller.admin.sourceapplydetail;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.dr.controller.admin.sourceapplydetail.vo.SourceApplyDetailPageReqVO;
import cn.iocoder.yudao.module.dr.controller.admin.sourceapplydetail.vo.SourceApplyDetailRespVO;
import cn.iocoder.yudao.module.dr.controller.admin.sourceapplydetail.vo.SourceApplyDetailSaveReqVO;
import cn.iocoder.yudao.module.dr.dal.dataobject.sourceapplydetail.SourceApplyDetailDO;
import cn.iocoder.yudao.module.dr.service.sourceapplydetail.SourceApplyDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 资源申请详情")
@RestController
@RequestMapping("/dr/source-apply-detail")
@Validated
public class SourceApplyDetailController {

    @Resource
    private SourceApplyDetailService sourceApplyDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建资源申请详情")
    @PreAuthorize("@ss.hasPermission('dr:source-apply-detail:create')")
    public CommonResult<Long> createSourceApplyDetail(@Valid @RequestBody SourceApplyDetailSaveReqVO createReqVO) {
        return success(sourceApplyDetailService.createSourceApplyDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资源申请详情")
    @PreAuthorize("@ss.hasPermission('dr:source-apply-detail:update')")
    public CommonResult<Boolean> updateSourceApplyDetail(@Valid @RequestBody SourceApplyDetailSaveReqVO updateReqVO) {
        sourceApplyDetailService.updateSourceApplyDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资源申请详情")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dr:source-apply-detail:delete')")
    public CommonResult<Boolean> deleteSourceApplyDetail(@RequestParam("id") Long id) {
        sourceApplyDetailService.deleteSourceApplyDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资源申请详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dr:source-apply-detail:query')")
    public CommonResult<SourceApplyDetailRespVO> getSourceApplyDetail(@RequestParam("id") Long id) {
        SourceApplyDetailDO sourceApplyDetail = sourceApplyDetailService.getSourceApplyDetail(id);
        return success(BeanUtils.toBean(sourceApplyDetail, SourceApplyDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资源申请详情分页")
    @PreAuthorize("@ss.hasPermission('dr:source-apply-detail:query')")
    public CommonResult<PageResult<SourceApplyDetailRespVO>> getSourceApplyDetailPage(@Valid SourceApplyDetailPageReqVO pageReqVO) {
        PageResult<SourceApplyDetailDO> pageResult = sourceApplyDetailService.getSourceApplyDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SourceApplyDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资源申请详情 Excel")
    @PreAuthorize("@ss.hasPermission('dr:source-apply-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSourceApplyDetailExcel(@Valid SourceApplyDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SourceApplyDetailDO> list = sourceApplyDetailService.getSourceApplyDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资源申请详情.xls", "数据", SourceApplyDetailRespVO.class,
                        BeanUtils.toBean(list, SourceApplyDetailRespVO.class));
    }

}